import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter/src/widgets/placeholder.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:image_preview/image_preview.dart';
import 'package:yt_devops/components/network_image_sll.dart';
import 'package:yt_devops/configs.dart';
import 'package:yt_devops/model/info_item.dart';
import 'package:yt_devops/resources/resources.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/extensions.dart';
import 'package:yt_devops/utils/func_utils.dart';
import 'package:yt_devops/utils/map_utils.dart';

class BasicInfoItemWidget extends StatelessWidget {
  final InfoItem item;

  final Widget? rightIconBuilder;
  const BasicInfoItemWidget(
    this.item, {
    super.key,
    this.rightIconBuilder,
  });

  @override
  Widget build(BuildContext context) {
    // 计算值是否单独在一行
    var single = isSingleRow(context, item.value);
    // 图片
    return item.type == InfoType.img
        ? Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              getTitleWidget(item.label),
              Expanded(child: imgGalleryWidget(context, item.value)),
            ],
          )
        : single
            ? Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  getTitleWidget(item.label),
                  const SizedBox(height: 2),
                  Row(
                    children: [
                      Expanded(
                        child: BrnExpandableText(
                          text: item.value,
                          maxLines: 2,
                          textStyle: TextStyles.f14.copyWith(height: 1.5),
                        ),
                      ),
                      rightIconBuilder ??
                          Offstage(
                            offstage: item.type != InfoType.address,
                            child: InkWell(
                              onTap: () {
                                if (item.latLng != null) {
                                  MapUtils.chooseMap(
                                    context,
                                    address: item.value,
                                    longitude: item.latLng!.longitude,
                                    latitude: item.latLng!.latitude,
                                  );
                                }
                              },
                              child: Container(
                                alignment: Alignment.centerRight,
                                width: 24.w,
                                child: Image.asset(
                                  ImageAssets.iconDingwei,
                                  width: 16.w,
                                  height: 16.w,
                                ),
                              ),
                            ),
                          ),
                    ],
                  )
                ],
              )
            : Row(
                children: [
                  getTitleWidget(item.label),
                  Expanded(
                    child: Text(
                      item.value,
                      style: TextStyle(
                          fontSize: 14.sp, color: Colour.mainTextColor),
                      textAlign: TextAlign.end,
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                  ),
                  rightIconBuilder ??
                      Offstage(
                        offstage: item.type != InfoType.address,
                        child: InkWell(
                          onTap: () {
                            if (item.latLng != null) {
                              MapUtils.chooseMap(
                                context,
                                address: item.value,
                                longitude: item.latLng!.longitude,
                                latitude: item.latLng!.latitude,
                              );
                            }
                          },
                          child: Container(
                            alignment: Alignment.centerRight,
                            width: 24.w,
                            child: Image.asset(
                              ImageAssets.iconDingwei,
                              width: 16.w,
                              height: 16.w,
                            ),
                          ),
                        ),
                      )
                ],
              );
  }

  Widget imgGalleryWidget(BuildContext context, String value) {
    List<String> imgList = [];
    if (!FuncUtils.isEmpty(value)) {
      imgList = value.split(',');

      imgList.removeWhere((img) => FuncUtils.isEmpty(img));

      imgList = imgList.map((e) => '${Configs.enviromentModel.api}$e').toList();
    }
    return Wrap(
        children: imgList
            .mapIndexed((e, i) => GestureDetector(
                child: Padding(
                  padding:
                      EdgeInsets.only(right: i % 2 == 0 ? 8.w : 0, bottom: 4),
                  child: Image.network(
                    e,
                    width: 76.w,
                    height: 76.w,
                    fit: BoxFit.cover,
                    errorBuilder: (context, error, stackTrace) => Image.asset(
                      ImageAssets.networkImageLoadingFailure,
                    ),
                  ),
                ),
                onTap: () {
                  openImagesPage(
                    Navigator.of(context),
                    imgUrls: imgList,
                    index: i,
                  );
                }))
            .toList());
  }

  getTitleWidget(String label) {
    return SizedBox(
      width: 96.w,
      child: Text(label,
          style: TextStyles.lf14.copyWith(
            height: 2.2,
          )),
    );
  }

  isSingleRow(BuildContext context, String value) {
    var maxValueWidth = MediaQuery.of(context).size.width - 32.w - 96.w;
    var textWidth = FuncUtils.calculateTextHeight(
        context, value, FontWeight.normal, 14.sp, double.infinity, 1);
    return textWidth > maxValueWidth;
  }
}
